package org.smi.test;

import java.util.concurrent.Semaphore;

/**
 * @Author: ism
 * @Platform: Mac
 * @Date: 4:07 PM 2019/2/22
 */
public class Worker extends Thread {

    private int num;
    private Semaphore semaphore;

    public Worker(int num, Semaphore semaphore) {
        this.num = num;
        this.semaphore = semaphore;
    }

    @Override
    public void run() {
        try {
            semaphore.acquire();
            System.out.println("工人 ->" + this.num + "<-占用一个机器在生产。。。");
            Thread.sleep(1000);
            System.out.println("工人 ->" + this.num + "<-释放机器\n");
            semaphore.release();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
